from tensorflow.keras.models import load_model
import cv2


class Recognizer:

    __model__ = None

    def __init__(self, model_path='model.keras'):
        self.__model__ = load_model(model_path)

    def analyze(self, image_path='./image/0.png'):

        # 读取图片
        img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

        # 二值化
        ret, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)

        # 缩放
        img = cv2.resize(img, (28, 28))

        # 转换为四维
        img = img.reshape(1, 28, 28, 1)

        # 预测
        result = self.__model__.predict(img)

        return result, result.argmax()
